package com.weishaoying.example.base.aware;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author WeiShaoying
 * @date 2018/4/4
 */
@Component(value = "魏韶颖")//awareService
public class AwareService implements BeanNameAware, ResourceLoaderAware {
    private String beanName;
    private ResourceLoader loader;

    @Override
    public void setBeanName(String name) {
        System.out.println("Spring容器正在调用BeanNameAware.setBeanName():" + name);
        this.beanName = name;
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        System.out.println("Spring容器正在调用ResourceLoaderAware.setResourceLoader()");
        this.loader = resourceLoader;
    }

    public void print() throws IOException {
        System.out.println("beanName = " + beanName);
        //利用Spring注入的Loader干活
        Resource resource = loader.getResource("classpath:el/book.txt");
        String s = IOUtils.toString(resource.getInputStream());
        System.out.println("s = " + s);
    }
}
